package crm.model.studentPC;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

import javax.swing.JComponent;
import javax.swing.JFrame;

import crm.model.Setting;

public class Stream {
	static ScreenshotComponent screen;
	static ObjectInputStream is;

	public static void show(ObjectInputStream is){
		createGUI();
		Stream.is=is;
		start();
	}
	private static void createGUI() {
		JFrame f = new JFrame ();
		f.setExtendedState(JFrame.MAXIMIZED_BOTH);
		screen = new ScreenshotComponent();
		f.setContentPane(screen);
		f.setVisible(true);

	}




	public static void start()  {
		try {

				DatagramSocket udpSocket = new DatagramSocket(Integer.parseInt(Setting.getString("Net.CommPort")));
				udpSocket.setBroadcast(true);
				byte[] buffer = new byte[1];
				DatagramPacket packet = new DatagramPacket(buffer, buffer.length);


				udpSocket.receive(packet);

				buffer = packet.getData();
				int lenght = buffer[0];

				int i = 0;
				byte [] imgArr = new byte[lenght];
				while (i < lenght){



					udpSocket.receive(packet);

					buffer = packet.getData();
					imgArr[i] = (buffer[0]);
				}

				Image img = Toolkit.getDefaultToolkit().createImage(imgArr);

				screen.img = img;
				System.out.println("repaint");
				screen.repaint();

		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SocketException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
class ScreenshotComponent extends JComponent {
	Image img;
	public void paint(Graphics g) {
		g.fillRect(0, 0, getWidth(), getHeight());
		g.drawImage(img, 0, 0,getWidth(),getHeight(),this);
	}
}
